﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data.SqlClient;
using System.Data;
using System.IO;
using Shl01.Models;

namespace Shl01.Dal
{
    [Serializable]
    public class Class1
    {
        /// <summary>
        /// 实体类
        /// </summary>
        /// <param name="con">数据库模型</param>
        /// <param name="DatabaseName">要连接的数据库名称</param>
        /// <param name="Path">实体类保存到的地址</param>
        /// <param name="TableName">要实体的表集合</param>
        /// <exception cref="有异常则抛出"></exception>
        public void Ma(Conn con, string DatabaseName, string Path, List<string> TableName)
        {
            string ConnStr = "";
            if (con.Type == 0)
            {
                ConnStr = string.Format("serVer = {0}; database = {3};uid = {1};pwd = {2}", con.Fuwu, con.Id, con.Pwd,DatabaseName);
            }
            else
            {
                ConnStr = string.Format("Data Source={0};Initial Catalog={1};Integrated Security=True", con.Fuwu,DatabaseName);
            }
            using (SqlConnection conn = new SqlConnection(ConnStr))
            {
                conn.Open();
                foreach (string tableName in TableName)
                {
                    using (SqlCommand cmd = new SqlCommand(string.Format("sp_columns"), conn))
                    {
                        cmd.CommandType = CommandType.StoredProcedure;
                        cmd.Parameters.Add(new SqlParameter("@table_name", tableName));
                        DataSet ds = new DataSet();
                        SqlDataAdapter adapter = new SqlDataAdapter(cmd);
                        adapter.Fill(ds);
                        DataTable dt = ds.Tables[0];
                        string Type = "";
                        string paras = "";
                        foreach (DataRow dr in dt.Rows)
                        {
                            string type = dr["TYPE_NAME"].ToString();
                            switch (type)
                            {
                                case "char":
                                    type = "string";
                                    break;
                                case "nchar":
                                    type = "string";
                                    break;
                                case "varchar":
                                    type = "string";
                                    break;
                                case "nvarchar":
                                    type = "string";
                                    break;
                                case "int":
                                    type = "int";
                                    break;
                                case "smallint":
                                    type = "int";
                                    break;
                                case "datetime":
                                    type = "DateTime";
                                    break;
                                case "float":
                                    type = "float";
                                    break;
                                case "smalldatetime":
                                    type = "DateTime";
                                    break;
                                case "bit":
                                    type = "bool";
                                    break;
                                case "decimal":
                                    type = "double";
                                    break;
                                case "money":
                                    type = "double";
                                    break;
                                case "int identity":
                                    type = "int";
                                    break;
                                case "ntext":
                                    type = "string";
                                    break;
                            }
                            string str = dr["COLUMN_NAME"].ToString();
                            Type += "public " + type + " " + str + @" { get; set; }
        ";
                            paras += "\r\n                                new SqlParameter(\"@" + str + "\",this." + str + "),";
                            paras.Replace("'", "\"");
                        }
                        string Class = @"using System.Collections.Generic;
using System;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data;
using System.Data.SqlClient;

namespace " + DatabaseName + ".Models" + @"
{
    [Serializable]
    public class " + tableName + @"
    {
        " + Type + @"
    }
}";

                        using (FileStream fs = new FileStream(Path + "\\" + tableName + ".cs", FileMode.Create))
                        {
                            using (StreamWriter sw = new StreamWriter(fs))
                            {
                                sw.WriteLine(Class);
                            }
                        }

                    }
                }
            }
        }
    }
}
